﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.BoM;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.BoM
{
    /// <summary>
    /// 物料清单显示Handler
    /// </summary>
    public class SearchBOMHandler : IRequestHandler<SearchBoMCommand, Result<List<BOMDto>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 物料清单表仓储
        /// </summary>
        public readonly IBaseRepository<BoMInform> boMInformrepository;
        /// <summary>
        /// 物料仓储
        /// </summary>
        public readonly IBaseRepository<MaterialManagement> materialManagementrepository;
        /// <summary>
        /// 物料单位仓储
        /// </summary>
        public readonly IBaseRepository<MaterialUnit> materialUnitrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchBOMHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="boMInformrepository"></param>
        /// <param name="logger"></param>
        /// <param name="db"></param>
        /// <param name="materialManagementrepository"></param>
        /// <param name="materialUnitrepository"></param>
        public SearchBOMHandler(IMapper mapper, IBaseRepository<BoMInform> boMInformrepository, ILogger<SearchBOMHandler> logger, ISqlSugarClient db, IBaseRepository<MaterialManagement> materialManagementrepository, IBaseRepository<MaterialUnit> materialUnitrepository)
        {
            this.mapper = mapper;
            this.boMInformrepository = boMInformrepository;
            this.logger = logger;
            _db = db;
            this.materialManagementrepository = materialManagementrepository;
            this.materialUnitrepository = materialUnitrepository;
        }
        /// <summary>
        /// 物料清单显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<List<BOMDto>>> Handle(SearchBoMCommand request, CancellationToken cancellationToken)
        {

            var result = new Result<List<BOMDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var query = await _db.Queryable<BoMInform, MaterialManagement,MaterialUnit>((p, t, u) => new JoinQueryInfos(
                    JoinType.Inner, p.MaterialId == t.MaterialId,
                    JoinType.Inner, t.MaterialUnitId == u.MaterialUnitId
                    )).Select((p, t, u) => new BOMDto()
                     {
                         BoMInformId = p.BoMInformId,
                         BoMInformCode = p.BoMInformCode,
                         BoMInformDec = p.BoMInformDec,
                         BoMInformVersion = p.BoMInformVersion,
                         MaterialId=t.MaterialId,
                         MaterialCode=t.MaterialCode,
                         MaterialName=t.MaterialName,
                         MaterialUnitName=u.MaterialUnitName,
                         Specifications=t.Specifications,
                         CreatedOwner = p.CreatedOwner,
                         CreatedTime = p.CreatedTime,
                         IsStatus = p.IsStatus,
                         IsDel = p.IsDel,
                     }).ToListAsync();
                result.Data = query;
                result.Msg = "物料清单显示查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "物料清单显示查询失败" + ex.Message;
                logger.LogError("物料清单显示查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
